//
//  DXWSearchViewController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/8/22.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWSearchViewController.h"
#import "UIBarButtonItem+DXWBarButtonItem.h"
#import "DXWHistorySearchView.h"


#define KfullPath  [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"history.plist"]

@interface DXWSearchViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 搜索文本输入框 */
@property (nonatomic, strong) UITextField *searchTF;
/** 存储搜索记录 */
@property (nonatomic, strong) NSMutableArray *historyMessages;

/** 提示框的占位视图 */
@property (weak, nonatomic) IBOutlet UIView *middleView;


@end

@implementation DXWSearchViewController

static NSString *ID = @"historyCell";

-(NSMutableArray *)historyMessages
{
    if (_historyMessages == nil) {
        
        _historyMessages = [NSMutableArray array];
        
    }
    return _historyMessages;
}
//- (void)awakeFromNib
//{
//    [super awakeFromNib];
//    _middleView.layer.cornerRadius = 10;
//    _middleView.layer.masksToBounds = YES;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    //1. 设置右侧搜索按钮
    [self setUpRightBtn];
    
    //2. 设置导航条中部
    [self setUpTitleView];
    
    _historyMessages = [NSMutableArray arrayWithContentsOfFile:KfullPath];
    NSLog(@"读取沙盒%@,%@",_historyMessages,KfullPath);
    if (_historyMessages != nil) {
        [_historyMessages insertObject:@"搜索历史" atIndex:0];
        DXWHistorySearchView *history = [DXWHistorySearchView historySearchView];
        history.frame = self.view.bounds;
        history.tableView.dataSource = self;
        history.tableView.delegate = self;
        [self.view addSubview: history];
    }
    
        
 



}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _historyMessages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
       cell = [[UITableViewCell alloc]init];
    }
    cell.textLabel.text = _historyMessages[indexPath.row];
    return cell;
}
#pragma mark -监听屏幕点击
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //点击屏幕,退出键盘
    [_searchTF resignFirstResponder];
}
#pragma mark -设置导航条rigtBtn
- (void)setUpRightBtn
{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"searchbutton"] higtLightedImage:[UIImage imageNamed:@"searchbutton_sel"] target:self action:@selector(searchBtnClick)];
}
- (void)searchBtnClick
{
    NSLog(@"搜索");
    NSString *historyText =  _searchTF.text;
    if (![historyText  isEqual: @""])
    {
        //将搜索记录写入沙盒
        [self.historyMessages addObject:historyText];
        NSLog(@"写入沙盒%@",KfullPath);
        [self.historyMessages writeToFile:KfullPath atomically:YES];
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            _middleView.alpha = 1.0;
            _middleView.hidden = NO;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 delay:1.0 options:0 animations:^{
            _middleView.alpha = 0.0;
            } completion:^(BOOL finished) {
//                [_noticeView removeFromSuperview];
            }];
      
        }];

    }

    NSLog(@"%@",_historyMessages);
    
}
#pragma mark -设置导航条middleView
- (void)setUpTitleView
{
    // middleView
    UIView *middleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW * 0.70, 34)];
    middleView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    
    // textField
    UITextField *searchTF = [[UITextField alloc]init];
    searchTF.frame = CGRectMake(dxw_margin_10, dxw_margin_5,middleView.frame.size.width - 2 * dxw_margin_5 , 30);
    [middleView addSubview:searchTF];
    searchTF.placeholder = @" 搜索房间ID,主播名称";
    searchTF.font = [UIFont systemFontOfSize:14];
    
    [searchTF addTarget:self action:@selector(searchTextFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
    
    [searchTF addTarget:self action:@selector(searchTextFieldEditingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
    
    searchTF.tintColor = [UIColor blackColor];
    searchTF.backgroundColor = [UIColor clearColor];
    _searchTF = searchTF;
    
    // clearBtn
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnW = 28;
    clearBtn.bounds = CGRectMake(0, 0, btnW, btnW);
    [clearBtn setImage:[UIImage imageNamed:@"search_clear_button"] forState:UIControlStateNormal];
    [clearBtn setImage:[UIImage imageNamed:@"search_clear_button_hover"] forState:UIControlStateHighlighted];
    [clearBtn addTarget:self action:@selector(clearPlaceholder) forControlEvents:UIControlEventTouchUpInside];
//    [clearBtn sizeToFit];
    [searchTF setRightView:clearBtn];
    searchTF.rightViewMode = UITextFieldViewModeWhileEditing;
    
    self.navigationItem.titleView = middleView;
}
#pragma mark -UITextField监听方法
- (void)searchTextFieldEditingDidBegin
{
      _searchTF.rightView.hidden = YES;
}
- (void)searchTextFieldEditingChanged
{
       _searchTF.rightView.hidden = NO;
        if (_searchTF.text.length != 0) {
            _searchTF.rightView.hidden = NO;
        }
    else
        _searchTF.rightView.hidden = YES;
}
- (void)clearPlaceholder
{
    _searchTF.text = nil;
    _searchTF.rightView.hidden = YES;
}





@end
